
clear
set more off
capture log close
set seed 12345
set matsize 10000
set maxvar 20000	
* set globals
global OUTPUT "$DIR/Output"
global DATA "$DIR"	
cd "$DIR"

/* SET LOCALS */
* set earliest tax year to keep
local yrb = 2008
* set last tax year to keep
local yre = 2016
* set excluded base year for event-time and controls
local baseyr "2012"
* log or level ("ln" "level")
local rlog "ln"
local rloglab "`rlog'"

* set DD year
local ddyear "2016"


****************************
**  Prep stayers data
****************************
use $DATA/scorp100p_entsep_file, clear
sort firm_tin tin year

gen zz = year==2016
by firm_tin tin: egen c2016 = sum(zz)
drop zz
gen stay2016 = c2016==1 & c2012==1
tab year stay2016
local yy "2016"
keep if year == 2012 | year==2011 | year==`yy'

* prep entrants, separators, stayers
sort firm_tin tin year
gen zz = year==2011
by firm_tin tin: egen c2011 = max(zz)
drop zz
gen stay = c2012==1 & c2011==1 & c`yy'==1
tab year stay

keep if stay==1

gen wages_ft=.
replace wages_ft=wages if ft==1

collapse (count) n_stay=tin (mean) wages wages_ft (sum) ft, by(firm_tin year)
ren wages wages_stay
ren wages_ft wages_stay_ft

sort firm_tin year
merge m:1 firm_tin year using $DATA/reg_file_firm.dta 
tab year _merge
keep if _merge==3
drop _merge


************************
**  Regression Analysis
**	 - Table 2
************************
local bm1 = `baseyr'-1
local bp1 = `baseyr'+1
local r1 "i.netinc_`baseyr'cat##ib`baseyr'.year"
local r2 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year"
local r3 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year"
local r4 "i.valaddpw_ft_`baseyr'cat##ib`baseyr'.year i.netinc_`baseyr'cat##ib`baseyr'.year i.naics_`baseyr'##ib`baseyr'.year i.cz_`baseyr'##ib`baseyr'.year"

local t "tt1112"
forval y = `yrb' / `yre' {
	drop t`y'
	gen t`y' = 0
	replace t`y' = . if `t'==.
	replace t`y' = `t' if year==`y'
}
local events "t`yy' y`yy' `t'"

drop lnwages lnwages_ft
gen lnwages=ln(wages_stay)
gen lnwages_ft = ln(wages_stay_ft)

gen zz = 0
replace zz = ft if year==2012
bys firm_tin: egen ft_2012=sum(zz)
drop zz

foreach r in 1 2 3 4 {
	eststo r`r': quietly areg lnwages_ft `events' `r`r'' [aw=ft_2012] if (`t'==1 | `t'==0) & (year==2011 | year==2012 | year==2016), absorb(firm_tin) vce(cluster firm_tin)
	}
esttab using "$OUTPUT/table2_panelB_stayers.csv", replace se ar2 mtitles( ) keep(t2016) 
eststo clear





clear
exit


